﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SocketUtil.Server
{

    public partial class MainForm : Form
    {

        private SocketNode Node { get; set; }

        public MainForm()
        {
            InitializeComponent();
            this.Load += MainForm_Load;
        }

        private string Title = "";

        private void MainForm_Load(object sender, EventArgs e)
        {
            Node = new SocketNode();
            Node.OnData += Node_OnData;
            Title = this.Text + " {0} ---> {1}";

            this.toolStripButton1.Enabled = false;
            this.toolStripButton2.Enabled = true;
        }

        private void Node_OnData(byte[] arg1, int arg2)
        {
            if (this.rtb_recv.InvokeRequired)
            {
                this.rtb_recv.Invoke(new Action<byte[], int>(Node_OnData), arg1, arg2);
                return;
            }
            this.rtb_recv.AppendText($"{arg1.ToStringEx(arg2)}");
        }

        /// <summary>
        /// 异步发送
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsb_send_Click(object sender, EventArgs e)
        {
            Node.SendAsync(tb_sender.Text, (s, e) => {
                Node_OnData(e.Buffer, e.Count);
            });
        }

        /// <summary>
        /// 绑定本地端口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            Node.BindLocal(tb_localIP.Text, int.Parse(tb_localPort.Text));
            this.Text = string.Format(Title, Node.LocalPoint.ToString(), "???");
        }

        /// <summary>
        /// 建立连接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            Node.Connect(tb_remoteIP.Text, int.Parse(tb_remotePort.Text));
            this.Text = string.Format(Title, Node.LocalPoint.ToString(), Node.RemotePoint.ToString());

            this.button3.Enabled = false;
            this.button2.Enabled = true;
            this.tb_remoteIP.Enabled = false;
            this.tb_remotePort.Enabled = false;
        }
        
        /// <summary>
        /// 断开连接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            Node.Disconnect();
            this.tb_remoteIP.Enabled = true;
            this.tb_remotePort.Enabled = true;
            this.button2.Enabled = false;
            this.button3.Enabled = true;
            this.Text = string.Format(Title, Node.LocalPoint.ToString(), "???");
        }

        /// <summary>
        /// 同步发送
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            Node.Send(tb_sender.Text);
        }

        /// <summary>
        /// 开始监听
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton2_Click_1(object sender, EventArgs e)
        {
            Node.StartListen();
            this.toolStripButton1.Enabled = true;
            this.toolStripButton2.Enabled = false;
        }

        /// <summary>
        /// 停止监听
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            Node.StopListen();
            this.toolStripButton1.Enabled = false;
            this.toolStripButton2.Enabled = true;
        }

        /// <summary>
        /// 断开连接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            Node.Disconnect();
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            Node.Reconnect();
        }
    }
}
